package com.example.xyr.myapplication.ui

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.view.View
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback
import cn.jpush.im.android.api.model.UserInfo
import com.example.xyr.myapplication.R
import com.example.xyr.myapplication.base.BaseActivity
import com.example.xyr.myapplication.model.BaseFriendBean
import com.example.xyr.myapplication.utils.LatlonUtils
import com.example.xyr.myapplication.utils.MyIntent
import com.example.xyr.myapplication.utils.TextUtil
import kotlinx.android.synthetic.main.activity_user_info.*
import kotlinx.android.synthetic.main.titlebar_layout.*

/**
 * Created by ylcf on 2018/9/7.
 */
class UserInfoActivity : BaseActivity(), View.OnClickListener {

    companion object {
        fun initActivity(fromActivity: Activity, bean: BaseFriendBean) {
            var intent = Intent(fromActivity, UserInfoActivity::class.java)
            MyIntent.instance.putExtra("bean", bean)
            fromActivity.startActivity(intent)
        }
    }

    override fun getLayout(): Int = R.layout.activity_user_info

    override fun init() {

    }

    override fun initListener() {
        titleBar.ivBack.setOnClickListener(this)
    }

    override fun initDate(intent: MyIntent) {
        titleBar.setTitle("好友资料")
        var bean: BaseFriendBean = intent.getExtra("bean", true)
        if (bean != null) {
            //用户ID
            var idStr = TextUtil.instance.setTextColor("ID：${bean.userInfo.userName}", resources.getColor(R.color.my_color_323232), 3)
            tv_info_id.text = idStr
            //昵称
            sup_info_name.setLeftString(bean.userInfo.nickname)
//            //位置
            LatlonUtils.getInstance(this).getStrAddress(bean.userInfo.address) { addresss ->
                sup_info_address.setLeftTextColor(resources.getColor(R.color.my_my_red))
                sup_info_address.setLeftString(addresss)
            }
            //性别
            var gender = bean.userInfo.gender
            when (gender) {
                UserInfo.Gender.female -> sup_info_name.setLeftIcon(R.drawable.d_nvhaier)
                UserInfo.Gender.male -> sup_info_name.setLeftIcon(R.drawable.d_nanhaier)
                else -> sup_info_name.setLeftIcon(R.drawable.d_aoteman)
            }
            //头像
            bean.userInfo.getAvatarBitmap(object : GetAvatarBitmapCallback() {
                override fun gotResult(p0: Int, p1: String?, p2: Bitmap?) {
                    if (p0 == 0) {
                        civ_info_avatar.setImageBitmap(p2)
                    }
                }

            })
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> finish()
        }
    }
}